package com.system.common.controller.purview;

import com.system.common.constant.BaseEnum;
import com.system.common.constant.SessionKeyConst;
import com.system.common.entity.purview.Purview;
import com.system.common.request.BaseReq;
import com.system.common.request.purview.SavePurviewReq;
import com.system.common.response.BaseResp;
import com.system.common.response.PageResp;
import com.system.common.service.purview.PurviewService;
import com.system.common.vo.person.PersonVo;
import com.system.common.vo.purview.PurviewVo;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Controller
@RequestMapping("/admin/purview")
public class PurviewController {

    @Autowired
    private PurviewService purviewService;

    @RequestMapping(value = "/list", method = {RequestMethod.GET})
    public String List() {
        return "/admin/purview/list";
    }


    @RequestMapping(value = "/queryAllPurview", method = {RequestMethod.GET})
    @ResponseBody
    public PageResp queryAllPurview(BaseReq req) {
        return purviewService.queryAllPurview(req);
    }

    @RequestMapping(value = "/save", method = {RequestMethod.POST})
    @ResponseBody
    public BaseResp save(SavePurviewReq req) {
        return purviewService.savePurview(req);
    }

    @RequestMapping(value = "/del", method = {RequestMethod.POST})
    @ResponseBody
    public BaseResp del(BaseReq req) {
        return purviewService.doDelPurview(req);
    }


    @RequestMapping(value = "/queryPurview", method = {RequestMethod.GET})
    @ResponseBody
    public BaseResp queryPurview(BaseReq req) {
        BaseResp resp = new BaseResp();
        if (req.getId() == null || req.getId() == 0) {
            resp.setCode(BaseEnum.RESULT_NULL.getCode());
            resp.setMsg(BaseEnum.RESULT_NULL.getMsg());
            return resp;
        }
        Purview purview = purviewService.getPurviewByPrimaryKey(req.getId());
        if(purview==null){
            resp.setCode(BaseEnum.RESULT_NULL.getCode());
            resp.setMsg(BaseEnum.RESULT_NULL.getMsg());
            return resp;
        }
        PurviewVo data = new PurviewVo();
        BeanUtils.copyProperties(purview, data);
        data.setLevel(purview.getPurLevel());
        resp.setCode(BaseEnum.SUCCESS.getCode());
        resp.setMsg(BaseEnum.SUCCESS.getMsg());
        resp.setData(data);
        return resp;
    }


    @RequestMapping(value = "/queryRolePurview", method = {RequestMethod.GET})
    @ResponseBody
    public PageResp queryRolePurview(BaseReq req){
        return purviewService.queryRolePurview(req);
    }

    @RequestMapping(value = "/queryMenu", method = {RequestMethod.GET})
    @ResponseBody
    public PageResp queryMenu(HttpServletRequest request){
        PageResp resp = new PageResp();
        Object data = request.getSession().getAttribute(SessionKeyConst.PERSON_MENU);
        if(data==null){
            resp.setCode(BaseEnum.PERSON_NOT_LOGIN.getCode());
            resp.setMsg(BaseEnum.PERSON_NOT_LOGIN.getMsg());
            return resp;
        }
        resp.setCode(BaseEnum.SUCCESS.getCode());
        resp.setData(data);
        return resp;
    }
}
